package com.juku2024.juku.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import com.juku2024.juku.R
/**
 * loading widget
 */
class LoadingView(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) {

    private val loading: ProgressBar
    private val errorLayout: View
    private val tvError: TextView
    private val ivError: ImageView
    private val btnRetry: TextView

    init {
        val rootView = LayoutInflater.from(context).inflate(R.layout.view_loading, this, true)
        loading = rootView.findViewById(R.id.loading)
        errorLayout = rootView.findViewById(R.id.errorLayout)
        tvError = rootView.findViewById(R.id.tv_error)
        ivError = rootView.findViewById(R.id.iv_error)
        btnRetry = rootView.findViewById(R.id.btn_retry)
    }

    fun showLoading() {
        visibility = View.VISIBLE
        loading.visibility = View.VISIBLE
        errorLayout.visibility = View.GONE
    }

    fun hideLoading() {
        visibility = View.GONE
        loading.visibility = View.GONE
        errorLayout.visibility = View.GONE
    }

    fun showErrorStatus(errorMsg: String, btnStr: String, listener: OnClickListener) {
        visibility = View.VISIBLE
        loading.visibility = View.GONE
        errorLayout.visibility = View.VISIBLE

        tvError.text = errorMsg
        btnRetry.visibility = View.VISIBLE
        btnRetry.text = btnStr
        btnRetry.setOnClickListener(listener)
    }

    fun hideErrorStatus(){
        visibility = View.GONE
        loading.visibility = View.GONE
        errorLayout.visibility = View.GONE

    }

    fun showEmptyStatus(msg: String) {
        visibility = View.VISIBLE
        loading.visibility = View.GONE
        errorLayout.visibility = View.VISIBLE

        btnRetry.visibility = View.GONE

        ivError.visibility = View.VISIBLE
        tvError.text = msg
    }

}